package tb4coding.sprider4lagou;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.alibaba.fastjson.JSON;

import tb4coding.sprider4lagou.dao.IResultDao;
import tb4coding.sprider4lagou.pojo.Lagou;
import tb4coding.sprider4lagou.pojo.Result;
import tb4coding.sprider4lagou.util.GetSessionFactory;
import tb4coding.sprider4lagou.util.HttpPostUtil;
import tb4coding.sprider4lagou.util.UrlConstants;

public class CatalogMain {

	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		String postResultStr = "";
		Lagou lagou = new Lagou();
		Map<String, String> postParam = new HashMap<String, String>();
		postParam.put("first", "true");
		postParam.put("kd", "java");
		postParam.put("pn", "1");
		postResultStr = HttpPostUtil.doPost(UrlConstants.YX_NO_CITY_ZZ, postParam);
		if(!"".equals(postResultStr)){
			lagou = JSON.parseObject(postResultStr, Lagou.class);
			Integer totalPageCount = lagou.getContent().getTotalPageCount();
			System.out.println("[LOG]------共搜索到" + lagou.getContent().getTotalCount() + "条招聘信息");
			System.out.println("[LOG]------共搜索到" + totalPageCount + "页");
			System.out.println("[LOG]------搜索信息每页" + lagou.getContent().getPageSize() + "条");
			System.out.println("[LOG]------开始主页爬取招聘信息------");
			
			SqlSession session = GetSessionFactory.getInstace().getSessionFactory().openSession();
			for (Integer i = 0; i < totalPageCount; i++) {
				postParam.put("first", "false");
				postParam.put("kd", "java");
				postParam.put("pn", String.valueOf(i+1));
				postResultStr = HttpPostUtil.doPost(UrlConstants.YX_NO_CITY_ZZ, postParam);
				if(!"".equals(postResultStr)){
					System.out.println("[LOG]------请求第"+ (i+1) +"页信息成功！");
					lagou = JSON.parseObject(postResultStr, Lagou.class);
					List<Result> resultList = lagou.getContent().getResult();
					for(Result result : resultList){
						
						session.getMapper(IResultDao.class).add(result);
					}
				}else{
					System.out.println("[LOG]------请求第"+ (i+1) +"页时，结果为空！");
				}
			}
			session.commit();
			session.close();
		}else{
			System.out.println("[LOG]------初次请求返回为空！");
		}
		long endTime = System.currentTimeMillis();
		Calendar useTime = Calendar.getInstance();
		useTime.setTimeInMillis(endTime - startTime);
		System.out.println("[LOG]------本次爬取共耗时：" + useTime.get(Calendar.MINUTE) + "分"+ useTime.get(Calendar.SECOND)+ "秒" + useTime.get(Calendar.MILLISECOND) + "毫秒");
	}

}
